<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>物料出入库统计</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        .layui-form-label {
            width: 100px;
        }
        .layui-input-inline {
            width: 200px;
        }
        .layui-table-cell {
            text-align: center;
        }
        .type-tabs {
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!-- 物料类型标签页 - 修复部分 -->
        <div class="type-tabs">
            <div class="layui-tab" lay-filter="materialTypeTabs">
                <ul class="layui-tab-title">
                    <li class="layui-this">电缆</li>
                    <li>变电</li>
                    <li>导线</li>
                    <li>新品</li>
                </ul>
            </div>
        </div>

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">物料名称</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="materialName" placeholder="请输入物料名称">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">物料编号</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="materialCode" placeholder="请输入物料编号">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"
                                    lay-submit lay-filter="data-search-btn">
                                <i class="layui-icon">&#xe615;</i> 查询
                            </button>
                            <button type="reset" class="layui-btn layui-btn-danger">
                                <i class="layui-icon">&#xe669;</i> 重置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="materialInOutTable" lay-filter="materialInOutFilter"></table>

        <script type="text/html" id="tableToolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="export">导出Excel</button>
            </div>
        </script>
    </div>
</div>

<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            element = layui.element,
            util = layui.util;

        // 当前选中的物料类型
        var currentType = 'cable';

        // 监听标签切换 - 修复部分
        element.on('tab(materialTypeTabs)', function(data){
            var types = ['cable', 'transformer', 'wire', 'new'];
            currentType = types[data.index];
            reloadTable();
        });

        // 初始化表格
        function renderTable() {
            table.render({
                elem: '#materialInOutTable',
                url: '/stats/material/in-out?type=' + currentType,
                loading: true,
                toolbar: '#tableToolbar',
                parseData: function(res) {
                    return {
                        "code": 0,
                        "msg": "",
                        "count": res.length,
                        "data": res
                    };
                },
                cols: [[
                    {field: 'materialCode', title: '物料编号', width: 120, align: 'center', sort: true},
                    {field: 'materialName', title: '物料名称', width: 150, align: 'center'},
                    {field: 'inQuantity', title: '入库数量', width: 120, align: 'center', sort: true,
                        templet: function(d) {
                            return d.inQuantity + ' ' + getUnitByType(currentType);
                        }},
                    {field: 'outQuantity', title: '出库数量', width: 120, align: 'center', sort: true,
                        templet: function(d) {
                            return d.outQuantity + ' ' + getUnitByType(currentType);
                        }},
                    {field: 'updateDate', title: '更新日期', width: 180, align: 'center',
                        templet: function(d) {
                            return d.updateDate ? util.toDateString(d.updateDate, 'yyyy-MM-dd HH:mm:ss') : '';
                        }},
                    {field: 'remark', title: '备注', width: 200, align: 'center'}
                ]],
                page: true,
                skin: 'line',
                limit: 10,
                limits: [10, 20, 50]
            });
        }

        // 根据类型获取单位
        function getUnitByType(type) {
            var units = {
                'cable': '米',
                'transformer': '米',
                'wire': '米',
                'new': '吨'
            };
            return units[type] || '';
        }

        // 重新加载表格
        function reloadTable() {
            var materialName = $('#materialName').val();
            var materialCode = $('#materialCode').val();

            table.reload('materialInOutTable', {
                url: '/stats/material/in-out',
                where: {
                    type: currentType,
                    materialName: materialName,
                    materialCode: materialCode
                },
                page: {
                    curr: 1
                }
            });
        }

        // 搜索功能
        form.on('submit(data-search-btn)', function (data) {
            reloadTable();
            return false;
        });

        // 重置功能
        $('.layui-btn-danger').on('click', function() {
            $('#materialName').val('');
            $('#materialCode').val('');
            reloadTable();
            return false;
        });

        // 工具栏事件
        table.on('toolbar(materialInOutFilter)', function (obj) {
            if (obj.event === 'export') {
                var materialName = $('#materialName').val();
                var materialCode = $('#materialCode').val();

                var url = '/stats/material/export?type=' + currentType +
                    '&materialName=' + encodeURIComponent(materialName || '') +
                    '&materialCode=' + encodeURIComponent(materialCode || '');

                window.location.href = url;
            }
        });

        // 初始渲染表格
        renderTable();
    });
</script>
</body>
</html>